<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <el-icon size="48" color="#409eff"><Hospital /></el-icon>
        <h2>医院管理系统</h2>
        <p>Hospital Management System</p>
      </div>
      
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        @submit.prevent="handleLogin"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            size="large"
            :prefix-icon="User"
            clearable
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            size="large"
            :prefix-icon="Lock"
            show-password
            clearable
            @keyup.enter="handleLogin"
          />
        </el-form-item>
        
        <el-form-item prop="role">
          <el-select
            v-model="loginForm.role"
            placeholder="请选择登录角色"
            size="large"
            style="width: 100%"
          >
            <el-option label="管理员" :value="3" />
            <el-option label="医生" :value="2" />
            <!-- <el-option label="患者" :value="1" /> -->
          </el-select>
        </el-form-item>
        
        <el-form-item>
          <el-button
            type="primary"
            size="large"
            style="width: 100%"
            :loading="loading"
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
      
      <div class="login-footer">
        <el-divider>
          <span class="divider-text">快速登录</span>
        </el-divider>
        <div class="quick-login">
          <el-button 
            size="small" 
            type="info" 
            plain 
            @click="quickLogin('admin')"
          >
            管理员登录
          </el-button>
          <el-button 
            size="small" 
            type="success" 
            plain 
            @click="quickLogin('doctor')"
          >
            医生登录
          </el-button>
          <!-- <el-button 
            size="small" 
            type="warning" 
            plain 
            @click="quickLogin('patient')"
          >
            患者登录
          </el-button> -->
        </div>
        <p class="copyright">
          © 2024 医院管理系统 All Rights Reserved
        </p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const userStore = useUserStore()
const loginFormRef = ref()
const loading = ref(false)

// 登录表单数据
const loginForm = reactive({
  username: '',
  password: '',
  role: null
})

// 表单验证规则
const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' }
  ],
  role: [
    { required: true, message: '请选择登录角色', trigger: 'change' }
  ]
}

// 处理登录
const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  try {
    await loginFormRef.value.validate()
    loading.value = true
    
    // 使用Pinia store的登录方法
    const response = await userStore.login(loginForm)
    
    ElMessage.success('登录成功')
    
    // 根据角色跳转到不同页面
    if (userStore.isAdmin) {
      // 管理员跳转到管理端
      router.push('/dashboard')
    } else if (userStore.isDoctor) {
      // 医生跳转到医生端
      router.push('/registrations')
    } else {
      // 患者跳转到患者端
      router.push('/dashboard')
    }
  } catch (error) {
    console.error('登录失败:', error)
    ElMessage.error(error.message || '登录失败，请检查网络连接')
  } finally {
    loading.value = false
  }
}

// 快速登录
const quickLogin = (type) => {
  switch (type) {
    case 'admin':
      loginForm.username = 'admin'
      loginForm.password = '123456'
      loginForm.role = 3
      break
    case 'doctor':
      loginForm.username = 'doctor001'
      loginForm.password = '123456'
      loginForm.role = 2
      break
    case 'patient':
      loginForm.username = 'patient001'
      loginForm.password = '123456'
      loginForm.role = 1
      break
  }
  
  ElMessage.info(`已填入${type === 'admin' ? '管理员' : type === 'doctor' ? '医生' : '患者'}测试账号`)
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.login-box {
  width: 100%;
  max-width: 400px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 12px;
  padding: 40px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.login-header h2 {
  margin: 16px 0 8px 0;
  color: #303133;
  font-size: 28px;
  font-weight: 600;
}

.login-header p {
  margin: 0;
  color: #909399;
  font-size: 14px;
  font-weight: 300;
}

.login-form {
  margin-bottom: 20px;
}

.login-form .el-form-item {
  margin-bottom: 24px;
}

.login-footer {
  margin-top: 30px;
}

.divider-text {
  color: #909399;
  font-size: 12px;
  padding: 0 12px;
  background: rgba(255, 255, 255, 0.95);
}

.quick-login {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  margin: 20px 0;
}

.quick-login .el-button {
  flex: 1;
}

.copyright {
  text-align: center;
  color: #909399;
  font-size: 12px;
  margin: 20px 0 0 0;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-box {
    padding: 30px 20px;
    margin: 0 10px;
  }
  
  .login-header h2 {
    font-size: 24px;
  }
  
  .quick-login {
    flex-direction: column;
    gap: 10px;
  }
}
</style> 